package com.ykdl.member.kid.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;

import com.ykdl.member.kid.R;
/**
 * 格式化时间的工具类
 */
public class CalendarUtil {

	public int currentYear, currentMonth, currentDay = -1;
	public int currentMonthDays = 0, preMonthDays = 0;

	private Calendar calendar = Calendar.getInstance();
	private int[] days = new int[42];

	public int dayOfWeek;

	public CalendarUtil() {
		currentYear = calendar.get(Calendar.YEAR);
		currentMonth = calendar.get(Calendar.MONTH);
		currentDay = calendar.get(Calendar.DAY_OF_MONTH);

	}

	public static int getMonthDay(int year, int month) {
		int days = 0;
		switch (month) {
		case 0:
			return days = 31;
		case 1:
			if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
				return days = 29;
			} else {
				return days = 28;
			}
		case 2:
			return days = 31;
		case 3:
			return days = 30;
		case 4:
			return days = 31;
		case 5:
			return days = 30;
		case 6:
			return days = 31;
		case 7:
			return days = 31;
		case 8:
			return days = 30;
		case 9:
			return days = 31;
		case 10:
			return days = 30;
		case 11:
			return days = 31;
		}
		return days;
	}

	public static int getWeekByString(String date) {
		int week = 0;
		String[] dates = date.split("-");

		if (dates.length == 3) {
			int year = Integer.valueOf(dates[0]);
			int month = Integer.valueOf(dates[1]);
			int day = Integer.valueOf(dates[2]);

			Calendar calendar = Calendar.getInstance();
			calendar.set(Calendar.YEAR, year);
			calendar.set(Calendar.MONTH, month - 1);
			calendar.set(Calendar.DAY_OF_MONTH, day);

			week = calendar.get(Calendar.DAY_OF_WEEK);
		}

		return week;
	}

	// return 2000-10-10
	public static String getDateFromLong(long mimisecond) {
		Date date = new Date(mimisecond);
		String dateTime = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
				+ "-" + date.getDate();
		return dateTime;
	}

	public static String getDateForLong(long mimisecond) {
		Date date = new Date(mimisecond);
		String dateTime = (date.getYear() + 1900) + "." + (date.getMonth() + 1)
				+ "." + date.getDate();
		return dateTime;
	}

	// return 12.5
	public static float getFloatTimeFromLong(long mimisecond) {
		Date d = new Date(mimisecond);

		return Float.parseFloat(String.format("%.1f",
				d.getHours() + 0.1f * d.getMinutes() / 60f));

	}

	// return 2000-10-10 20:00
	public static String getNowTime() {
		Date d = new Date(System.currentTimeMillis());
		return d.getYear() + 1900 + "-" + (d.getMonth() + 1) + "-"
				+ d.getDate() + " " + d.getHours() + ":" + d.getMinutes();
	}

	public static String getTimeCurrentTime(int[] i) {
		String hour = i[2] < 10 ? "0" + i[2] : i[2] + "";
		String minutes = i[3] < 10 ? "0" + i[3] : i[3] + "";

		return i[0] + "月" + i[1] + "日" + " " + hour + ":" + minutes;
	}

	public static String getTimeCurrentTime(String[] i) {
		String hour = i[2].length() == 1 ? "0" + i[2] : i[2];
		String minutes = i[3].length() == 1 ? "0" + i[3] : i[3];
		return i[0] + "月" + i[1] + "日" + " " + hour + ":" + minutes;
	}

	

	public static String formatDate(long mAndD) {
		Date date = new Date(mAndD);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

	public static String formatTime(long mAndD) {
		Date date = new Date(mAndD);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateFormat.format(date);
	}
	
	public static String getFormatDate(String year, String month, String day,
			String hour, String minutes) {
		return getFormatDate(year, month, day) + "  " + hour + ":" + minutes;
	}

	public static String getFormatDate(String year, String month, String day) {
		return year + "-" + month + "-" + day;
	}

	public static boolean isSameDay(long oneTime, long twoTime) {
		Date one = new Date(oneTime);
		Date two = new Date(twoTime);
		if ((one.getYear() == two.getYear())
				&& (one.getMonth() == two.getMonth())
				&& (one.getDate() == two.getDate())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 适合格式:month+"月"+day+"日"
	 * 
	 * @param date
	 * @return 0:month 1:day
	 */
	public static String[] getMonthAndDay(String date) {
		String[] temp = date.split("月");
		String month = null;
		String day = null;
		if (temp != null && temp.length > 1) {
			month = temp[0];
			day = temp[1].split("日")[0];
		} else {
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			month = (calendar.get(Calendar.MONTH) + 1) + "";
			day = (calendar.get(Calendar.DAY_OF_MONTH)) + "";
		}
		return new String[] { month, day };
	}

	public int[] getCaculateDays(int year, int month) {
		calendar.clear();
		// calendar.set(year,month, 1);
		// dayOfWeek=calendar.get(Calendar.DAY_OF_WEEK);
		dayOfWeek = getCurrentDayOfWeek(year, month);
		currentMonthDays = getMonthDay(year, month);
		if (month == 0) {
			preMonthDays = getMonthDay(year - 1, 11);
		} else {
			preMonthDays = getMonthDay(year, month - 1);
		}
		// 把当月的天数放到数组中
		int currMonthDays = 1;
		for (int i = dayOfWeek - 1; i < currentMonthDays + dayOfWeek - 1; i++) {
			days[i] = currMonthDays;
			currMonthDays++;
		}

		// 把上一个月的天数补齐前边的空的数组
		for (int i = dayOfWeek - 2; i >= 0; i--) {
			days[i] = preMonthDays;
			preMonthDays--;
		}

		// 把下一个月的天数补齐后边的数组
		int nextMonthDays = 1;
		for (int i = currentMonthDays + dayOfWeek - 1; i < 42; i++) {
			days[i] = nextMonthDays;
			nextMonthDays++;
		}
		return days;
	}

	public long getLongByString(String month, String day, String hour,
			String min) {
		int month_i = Integer.valueOf(month);
		int day_i = Integer.valueOf(day);
		int hour_i = Integer.valueOf(hour);
		int min_i = Integer.valueOf(min);

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, getCurrentYear());
		calendar.set(Calendar.MONTH, month_i - 1);
		calendar.set(Calendar.DAY_OF_MONTH, day_i);
		calendar.set(Calendar.HOUR_OF_DAY, hour_i);
		calendar.set(Calendar.MINUTE, min_i);
		return calendar.getTimeInMillis();
	}
	
	public static long getLongByString(String year,String month, String day, String hour,
			String min) {
		int year_i = Integer.valueOf(year);
		int month_i = Integer.valueOf(month);
		int day_i = Integer.valueOf(day);
		int hour_i = Integer.valueOf(hour);
		int min_i = Integer.valueOf(min);

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year_i);
		calendar.set(Calendar.MONTH, month_i - 1);
		calendar.set(Calendar.DAY_OF_MONTH, day_i);
		calendar.set(Calendar.HOUR_OF_DAY, hour_i);
		calendar.set(Calendar.MINUTE, min_i);
		return calendar.getTimeInMillis();
	}

	public int getCurrentDayOfWeek(int cyear, int cmonth) {
		calendar.set(cyear, cmonth, 1);
		int cdayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return cdayOfWeek;
	}

	public int getCurrentDayOfYear() {
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	public int getCurrentYear() {
		return currentYear;
	}

	public int getDayInYear(int year) {
		int allYear;
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
			allYear = 366;
		} else {
			allYear = 365;
		}
		return allYear;
	}

	public int[] getCurrentTime() {
		int month = getCurrentMonth() + 1;
		int day = getCurrentDay();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int min = calendar.get(Calendar.MINUTE);

		return new int[] { month, day, hour, min };
	}

	public void setCurrentYear(int currentYear) {
		this.currentYear = currentYear;
	}

	public int getCurrentMonth() {
		return currentMonth;
	}

	public void setCurrentMonth(int currentMonth) {
		this.currentMonth = currentMonth;
	}

	public int getCurrentDay() {
		return currentDay;
	}

	public void setCurrentDay(int currentDay) {
		this.currentDay = currentDay;
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public int[] getDays() {
		return days;
	}

	public void setDays(int[] days) {
		this.days = days;
	}

	/**
	 * 
	 * @param psetvalue
	 *            2011-01-01 格式
	 * @param context
	 * @return 0正常 -1 早于出生日期，1 大于当前日期
	 */
	public static int isRightTime(String psetvalue, Context context) {
		// 当前时间
		/*try {
			Calendar current = Calendar.getInstance();
			Calendar babyBirth = Calendar.getInstance();
			Calendar setValue = Calendar.getInstance();

			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
			setValue.setTime(df1.parse(psetvalue));

			String babyBirthStr = MyPrefs.getBabyBirth(context);

			babyBirth.setTime(df1.parse(babyBirthStr));

			current.setTimeInMillis(System.currentTimeMillis());

			if (setValue.before(babyBirth)) {
				return -1;
			} else if (setValue.after(current)) {
				return 1;
			} else {
				return 0;
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}*/
		return 0;
	}
	
	/**
	 * 获取星座
	 * @param mMonth
	 * @param mDay
	 * @return
	 */
	public static String getConstellation(int mMonth, int mDay){
	String result="";
			 switch (mMonth) {
			 case 1:
			 if (mDay <= 19) {
			result=ResoureUtil.getResoure().getString(R.string.capricorn);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.aquarius);
			 }
			 break;
			 case 2:
			 if (mDay <= 18) {
			result=ResoureUtil.getResoure().getString(R.string.aquarius);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.pisces);
			 }
			 break;
			 case 3:
			 if (mDay <= 20) {
			result=ResoureUtil.getResoure().getString(R.string.pisces);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.aries);
			 }
			 break;
			 case 4:
			 if (mDay <= 19) {
			result=ResoureUtil.getResoure().getString(R.string.aries);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.taurus);
			 }
			 break;
			 case 5:
			 if (mDay <= 20) {
			result=ResoureUtil.getResoure().getString(R.string.taurus);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.gemini);
			 }
			 break;
			 case 6:
			 if (mDay <= 20) {
			result=ResoureUtil.getResoure().getString(R.string.gemini);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.cancer);
			 }
			 break;
			 case 7:
			 if (mDay <= 22) {
			result=ResoureUtil.getResoure().getString(R.string.cancer);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.leo);
			 }
			 break;
			 case 8:
			 if (mDay <= 22) {
			result=ResoureUtil.getResoure().getString(R.string.leo);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.virgo);
			 }
			 break;
			 case 9:
			 if (mDay <= 22) {
			result=ResoureUtil.getResoure().getString(R.string.virgo);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.libra);
			 }
			 break;
			 case 10:
			 if (mDay <= 23) {
			result=ResoureUtil.getResoure().getString(R.string.libra);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.scorpio);
			 }
			 break;
			 case 11:
			 if (mDay <= 21) {
			result=ResoureUtil.getResoure().getString(R.string.scorpio);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.sagittarius);
			 }
			 break;
			 case 12:
			 if (mDay <= 21) {
			result=ResoureUtil.getResoure().getString(R.string.sagittarius);
			 } else {
			result=ResoureUtil.getResoure().getString(R.string.capricorn);
			 }
			 break;
			 }
			 return result;


	}

}
